#include "Spi_Dev_Stub.h"
#include "Utilities/Utilities.h"
#include <thread>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <sstream>

#define PARAM_OFFSET(Strct, Field)    ((unsigned long)&(((Strct *)0)->Field))

Spi_Dev_Stub::Spi_Dev_Stub(const std::string& id) : its_id(id)
{

}

bool Spi_Dev_Stub::open_dev()
{
    return true;
}

bool Spi_Dev_Stub::init_dev([[maybe_unused]]uint32_t speed, [[maybe_unused]]uint16_t mode, [[maybe_unused]]uint8_t bits, [[maybe_unused]]uint16_t delay, [[maybe_unused]]bool verbose) 
{
    // LOG(INFO) << "node : " << its_id << " "
    //           << "speed : " << speed << " "
    //           << "mode : " << mode << " "
    //           << "bits : " << bits << " "
    //           << "delay : " << delay << " "
    //           << "verbose : " << std::boolalpha << verbose
    //           << std::endl;
    return true;
}

void Spi_Dev_Stub::close_dev()
{
    
}

bool Spi_Dev_Stub::send_msg()
{
    return true;
}

bool Spi_Dev_Stub::recv_msg()
{
    return true;
}

bool Spi_Dev_Stub::handle_maxieye_test([[maybe_unused]]uint8_t const *tx, [[maybe_unused]]uint8_t const *rx, [[maybe_unused]]size_t len)
{
    // if(len == FRAME_SIZE_DF)
    // {   
    //     //soc->mcu
    //     {
    //         MET_SPI1_SOCTOMCU_MSG msg;
    //         std::copy_n(tx+10, sizeof(MET_SPI1_SOCTOMCU_MSG), reinterpret_cast<uint8_t*>(&msg));
    //         // Utility::hex_dump(tx+10+PARAM_OFFSET(MET_SPI1_SOCTOMCU_MSG, EHR_SPI_Out_T), sizeof(MET_SPI1_SOCTOMCU_MSG), "TX");
    //         LOG(INFO) << "SOC->MCU " << msg.EHR_SPI_Out_T.EHR_RecommendAccel << "   " << static_cast<uint16>(msg.EHR_SPI_Out_T.ByteAlignedFill[2]);
    //     }

    //     //mcu->soc
    //     {
    //         static float num = 1.24; 
    //         num += 1.0;
    //         static uint8_t u8_num = 0;
    //         u8_num++;
    //         MET_SPI1_MCUTOSOC_MSG msg;
    //         msg.RadarMsg.Header_Info.SensorPosition_LRR.SENSOR_Pitch = num;
    //         msg.RadarMsg.Long_Range_Radar_CAN_MSG.Reserved[851] = u8_num;
    //         uint8_t* p_payload = const_cast<uint8_t*>(rx)+10;
    //         std::copy_n(reinterpret_cast<uint8_t*>(&msg), sizeof(MET_SPI1_MCUTOSOC_MSG), p_payload);

    //         // printf("-----------------\n");
    //         // Utility::hex_dump(p_payload+PARAM_OFFSET(MET_SPI1_MCUTOSOC_MSG, RadarMsg), sizeof(LongRangeRadar_MSG), "rx");

    //         // LongRangeRadar_MSG rader;
    //         // uint8_t* tmp_rx1 = const_cast<uint8_t*>(rx)+10;
    //         // std::copy_n(tmp_rx1+PARAM_OFFSET(MET_SPI1_MCUTOSOC_MSG, RadarMsg), sizeof(LongRangeRadar_MSG), reinterpret_cast<uint8_t*>(&rader));
    //         // printf("%f  %x \n", rader.Header_Info.SensorPosition_LRR.SENSOR_Pitch, rader.Long_Range_Radar_CAN_MSG.Reserved[851]);
    //     }
    // }


    return false;
}


bool Spi_Dev_Stub::verify_Struct_EHR_SPI_Out_SOC_T([[maybe_unused]]uint8_t const *tx)
{
    // Struct_EHR_SPI_Out_SOC_T data;
    // constexpr int start_position = 7820;
    // constexpr int length = 28;

    // memcpy(reinterpret_cast<uint8_t*>(&data), tx+start_position, length);

    // return  data.EHR_Status == 0x01 &&
    //         data.EHR_NaviStatus == 0x02 &&
    //         data.EHR_RollingCnt == 0x03 &&
    //         data.EHR_SpdLmtKph == 0x04 &&
    //         data.EHR_SpdLmtReason == 0x05 &&
    //         data.EHR_Event_1 == 0x06 &&
    //         data.EHR_Event_2 == 0x07 &&
    //         data.EHR_Event_3 == 0x08 &&
    //         data.EHR_CurRoadType == 0x09 &&
    //         data.ByteAlignedFill[0] == 0x0a &&  
    //         data.ByteAlignedFill[1] == 0x0b && 
    //         data.ByteAlignedFill[2] == 0x0c; 
    return true;
}

bool Spi_Dev_Stub::transfer([[maybe_unused]]uint8_t const *tx, [[maybe_unused]]uint8_t const *rx, [[maybe_unused]]size_t len)
{
    // LOG(INFO) << len;
    // Utility::hex_dump(tx+7820, 28, 16, "Tx");
    //soc->mcu
    // if(verify_Struct_EHR_SPI_Out_SOC_T(tx))
    // {
    //     LongRangeRadar_MSG data2;
    //     data2.Header_Info.SensorPosition_LRR.SENSOR_Pitch = 1.11;
    //     data2.Header_Info.SensorPosition_LRR.SENSOR_Yaw = 1.12;
    //     data2.Header_Info.SensorPosition_LRR.BoresightAngleYaw = 1.13;
    //     data2.Header_Info.SensorPosition_LRR.SENSOR_LatPos = 0x1001;
    //     data2.Header_Info.SensorPosition_LRR.SENSOR_LongPos = 0x1002;
    //     data2.Header_Info.SensorPosition_LRR.SENSOR_VertPos = 0x1003;
    //     data2.Header_Info.SensorPosition_LRR.SENSOR_Orientation = 0x14;
    //     data2.Header_Info.SensorPosition_LRR.ByteAlignedFill = 0x15;
    //     for(uint8_t i = 0; i < 255; i++)
    //     {
    //         data2.Long_Range_Radar_CAN_MSG.Reserved[851-i] = i;
    //     }
    //     memcpy(const_cast<uint8_t*>(rx) + 484, reinterpret_cast<uint8_t*>(&data2), 2540);
    // }
    
    return true;
}